import wx from 'weixin-js-sdk'
import $api from "@/api/index";
import { Toast } from "vant";
export default {
    /**
     *
     * @param {*} api  微信公众号api
     * @param {*} fn 回调
     */
    getShareInfo(tit, fxUrl, fxImage, fxDesc) { //如果分享的内容会根据情况变化，那么这里可以传入分享标题及url
        console.log(fxImage)
            // 判断苹果手机
        let share_url = window.initUrl ? window.initUrl : location.href.split('#')[0];
        let sharearr = ['updateAppMessageShareData', 'updateTimelineShareData', 'onMenuShareWeibo'];
        let url = api.wechat.jssdk + '?f=' + handle.encryption({ "apis": sharearr, "url": share_url });

        /*
        let urls = ''
        if (window.__wxjs_is_wkwebview === true) {
            urls = window.location.href.split('#')[0] || window.location.href
        } else {
            urls = window.location.href
        }
        */
        $api.shopApi.shareConfig(url).then(res => {
            //console.log('签证相关' + JSON.stringify(res.data))
            //localStorage.setItem("jsapi_ticket", res.data.jsapi_ticket);
            //拿到后端给的这些数据
            let result_data = res.data;
            let appId = res.data.appId;
            let timestamp = res.data.timestamp;
            let nonceStr = res.data.nonceStr;
            let signature = res.data.signature;
            wx.config({
                debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。
                appId: appId, // 必填，公众号的唯一标识，填自己的！
                timestamp: timestamp, // 必填，生成签名的时间戳，刚才接口拿到的数据
                nonceStr: nonceStr, // 必填，生成签名的随机串
                signature: signature, // 必填，签名，见附录1
                jsApiList: [
                    'onMenuShareAppMessage',
                    'onMenuShareTimeline',
                    'updateAppMessageShareData',
                    'updateTimelineShareData',
                ]
            })
            wx.ready(function() {
                //分享给朋友
                wx.onMenuShareAppMessage({
                    title: tit,
                    desc: fxDesc,
                    link: result_data.url,
                    imgUrl: fxImage,
                    success: function() {
                        //Toast('分享成功')
                        console.log("分享成功");
                    },
                    cancel: function() {
                        //Toast('取消分享')
                        console.log("取消分享");
                    }
                });
                //分享到朋友圈
                wx.onMenuShareTimeline({
                    title: tit, // 分享时的标题
                    link: result_data.url, // 分享时的链接
                    imgUrl: fxImage, // 分享时的图标
                    success: function() {
                        //Toast('分享成功')
                        console.log("分享成功");
                    },
                    cancel: function() {
                        //Toast('取消分享')
                        console.log("取消分享");
                    }
                });

                wx.updateAppMessageShareData({
                    title: tit,
                    desc: fxDesc,
                    link: result_data.url,
                    imgUrl: fxImage
                });
                wx.updateTimelineShareData({
                    title: option.shareTitle,
                    desc: fxDesc,
                    link: result_data.url,
                    imgUrl: fxImage
                });
            })
        })
    }
}